//
//  RHHotProductCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/12.
//

#import "RHHotProductCell.h"
#import "RHHomeCollectionViewCell.h"

@interface RHHotProductCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *ListView;

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) NSArray *dataArr;

@end
@implementation RHHotProductCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.dataArr = @[@"home_hot01",@"home_hot02",@"home_hot03"];
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    self.ListView.backgroundColor = CFTabViewBgColor;
    [_ListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_offset(0);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(120);
    }];
    
}

#pragma mark  设置CollectionView的组数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    

    RHHomeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewCell" forIndexPath:indexPath];
    cell.productImgV.image = [UIImage imageNamed:self.dataArr[indexPath.item]];
    cell.contentView.backgroundColor = CFTabViewBgColor;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat itemSize = (kUI_WINDOW_W-4*15)/3;
    return CGSizeMake(itemSize, itemSize);
}

#pragma mark  设置CollectionViewCell是否可以被点击
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
//行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
   
    return 0;
}

//定义每个Section的四边间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);//分别为上、左、下、右
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.selectItemBlock){
        self.selectItemBlock(indexPath.item);
    }
}

#pragma mark  设置CollectionView的的参数
-(UICollectionView *)ListView{

   if (!_ListView) {

       _flowLayout = [[UICollectionViewFlowLayout alloc] init];
       _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
       _ListView = [[UICollectionView alloc] initWithFrame: CGRectMake(0,0, kUI_WINDOW_W, 120) collectionViewLayout: _flowLayout];
       _ListView.delegate = self;
       _ListView.dataSource = self;
       _ListView.bounces = YES;
       _ListView.showsHorizontalScrollIndicator = NO;
       _ListView.showsVerticalScrollIndicator = NO;
       _ListView.pagingEnabled = NO;
        [self.contentView addSubview:_ListView];
   
        [_ListView registerClass:[RHHomeCollectionViewCell class] forCellWithReuseIdentifier:@"HomeCollectionViewCell"];
        [_ListView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader"];
         [_ListView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionViewFooter"];

   }

    return _ListView;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
